<template>
  <div class="register-container">
    <div class="register-content">
      <div class="welcome-text">
        <h1>Create Account</h1>
        <p>欢迎加入智面宝典</p>
      </div>

      <div class="register-form">
        <div class="input-group">
          <label>用户名</label>
          <input type="text" v-model="registForm.username" placeholder="请输入5-16位字母、数字或下划线">
        </div>

        <div class="input-group">
          <label>密码</label>
          <input type="password" v-model="registForm.password" placeholder="请输入8-16位包含大小写字母和数字密码">
        </div>

        <div class="input-group">
          <label>确认密码</label>
          <input type="password" v-model="registForm.repassword" placeholder="请再次输入密码">
        </div>

        <div class="input-group">
          <label>邮箱</label>
          <input type="email" v-model="registForm.email" placeholder="请输入邮箱">
        </div>

        <div class="input-group">
          <label>手机号</label>
          <input type="tel" v-model="registForm.phone" placeholder="请输入手机号">
        </div>

        <div class="input-group">
          <label>专业</label>
          <select v-model="registForm.majorId">
            <option value="1">工学</option>
            <option value="2">软件工程</option>
          </select>
        </div>

        <button class="register-btn" @click="handleRegister">注册</button>
        <div class="login-link" @click="goToLogin">已有账号？立即登录</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { getUserRegist } from './api/register'

const router = useRouter()

const registForm = reactive({
  username: '',
  password: '',
  repassword: '',
  email: '',
  phone: '',
  majorId: '1', // 默认选择工学
  avatar: 'https://felixai.oss-cn-chengdu.aliyuncs.com/%E5%88%9B%E5%BB%BA%E6%99%BA%E8%83%BD%E9%9D%A2%E8%AF%95%E8%BD%AF%E4%BB%B6.png',
  role: 'user' // 默认用户角色
})

const handleRegister = async () => {
  // 用户名正则：5-16位字母、数字、下划线
  const usernameRegex = /^[a-zA-Z0-9_]{5,16}$/
  // 密码正则：8-16位字母、数字、特殊字符
  const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,16}$/
  // 邮箱正则
  const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
  // 手机号正则
  const phoneRegex = /^1[3-9]\d{9}$/

  if (!usernameRegex.test(registForm.username)) {
    showToast('用户名必须为5-16位字母、数字或下划线')
    return
  }
  if (!passwordRegex.test(registForm.password)) {
    showToast('密码必须为8-16位，包含大小写字母和数字')
    return
  }
  if (registForm.password !== registForm.repassword) {
    showToast('两次输入的密码不一致')
    return
  }
  if (!emailRegex.test(registForm.email)) {
    showToast('请输入正确的邮箱格式')
    return
  }
  if (!phoneRegex.test(registForm.phone)) {
    showToast('请输入正确的手机号格式')
    return
  }

  try {
    const registerData = {
      username: registForm.username,
      password: registForm.password,
      email: registForm.email,
      avatar: registForm.avatar,
      phone: registForm.phone,
      majorId: registForm.majorId,
      role: registForm.role
    }

    const res = await getUserRegist(registerData)
    if (res.data.code === 200) {
      showToast('注册成功')
      // 直接跳转到登录页面
      setTimeout(() => {
        router.push('/login')
      }, 1500)
    } else {
      showToast(res.data.message || '注册失败')
    }
  } catch (error) {
    console.error('注册错误:', error)
    showToast('注册请求失败')
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #e6f3ff 0%, #b6d9ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;

  .register-content {
    width: 100%;
    max-width: 400px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 24px;
    padding: 30px;
    box-shadow: 0 10px 30px rgba(31, 38, 135, 0.2);
    backdrop-filter: blur(8px);

    .welcome-text {
      text-align: center;
      margin-bottom: 30px;

      h1 {
        color: #333;
        font-size: 28px;
        font-weight: 600;
        margin-bottom: 8px;
      }

      p {
        color: #666;
        font-size: 16px;
      }
    }

    .register-form {
      .input-group {
        margin-bottom: 20px;

        label {
          display: block;
          color: #333;
          margin-bottom: 8px;
          font-size: 15px;
          font-weight: 500;
        }

        input {
          width: 100%;
          padding: 14px 16px;
          border: 1px solid #ddd;
          border-radius: 12px;
          font-size: 15px;
          transition: all 0.3s;
          background: #fff;

          &:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.15);
          }

          &::placeholder {
            color: #999;
            font-size: 14px;
          }
        }

        select {
          width: 100%;
          padding: 14px 16px;
          border: 1px solid #ddd;
          border-radius: 12px;
          font-size: 15px;
          transition: all 0.3s;
          background: #fff;
          appearance: none; // 移除默认的下拉箭头
          background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
          background-repeat: no-repeat;
          background-position: right 1rem center;
          background-size: 1em;

          &:focus {
            outline: none;
            border-color: #4a90e2;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.15);
          }
        }
      }

      .register-btn {
        width: 100%;
        padding: 14px;
        background: #4a90e2;
        color: white;
        border: none;
        border-radius: 12px;
        font-size: 16px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.3s;
        margin-top: 24px;
        box-shadow: 0 4px 12px rgba(74, 144, 226, 0.2);

        &:hover {
          background: #357abd;
          transform: translateY(-1px);
        }

        &:active {
          transform: scale(0.98);
        }
      }

      .login-link {
        text-align: center;
        margin-top: 20px;
        color: #4a90e2;
        cursor: pointer;
        font-size: 15px;
        font-weight: 500;

        &:hover {
          text-decoration: underline;
        }
      }
    }
  }
}

@media screen and (max-height: 600px) {
  .register-container {
    align-items: flex-start;
    padding: 16px;

    .register-content {
      padding: 24px;

      .welcome-text {
        margin-bottom: 24px;

        h1 {
          font-size: 24px;
        }

        p {
          font-size: 14px;
        }
      }
    }
  }
}
</style>